module repository.RedEnvelopeSplitRepository;

import bootstrap.DatabaseBootstrap;
import hunt.entity;
import hunt.logging;
import std.conv : to;
import model;
import message.MsgDefine;
import core.stdc.time;
import message.Constants;

import hunt.framework;

class RedEnvelopeSplitRepository : EntityRepository!( RedEnvelopeSplit , int){

  this()
  {
    super( defaultEntityManager());
  }

  void addRedEnvelopeSplit(SendRedEnvelopeReq req ,ulong userId, long amount, int id, EntityManager em)
  {
    auto insert = em.createQuery!(RedEnvelopeSplit)("INSERT INTO RedEnvelopeSplit u (u.envelopeId,u.amount,u.created,u.chatId,u.fromId) VALUES (:envelopeId,:amount,:created,:chatId,:fromId);");
    insert.setParameter("envelopeId",id);
    insert.setParameter("amount",amount);
    insert.setParameter("created",core.stdc.time.time(null));
    insert.setParameter("chatId",req.target_id);
    insert.setParameter("fromId",userId);
    insert.exec();
  }

  RedEnvelopeSplit getOne(int id)
  {
     return find(new Condition("%s = %s" , Field.envelopeId , id));
  }

  void deleRedEnvelopeSplit(RedEnvelopeSplit obj, EntityManager em)
  {
    auto update = em.createQuery!(RedEnvelopeSplit)("DELETE FROM RedEnvelopeSplit u WHERE u.id = :id");
    update.setParameter("id",obj.id);
    update.exec();
  }

}
